import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/models/repair_department_list_entity.dart';
import 'package:kkhousekeep/models/repair_staff_list_entity.dart';
import 'package:kkhousekeep/net/home_order_connect.dart';
import 'package:kkhousekeep/net/respnse_code.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';
import 'package:kkhousekeep/tools/toast_util.dart';

import '../../../../config/constant.dart';
import '../../../../models/order_list_entity.dart';
import '../../../../models/property_order_detail_entity.dart';
import '../../../public/take_picture_screen.dart';

class OrderTransferController extends GetxController {
  late OrderListObj _orderEntity;
  var orderId = 0;
  var woSn = "".obs;
  var fatalType = "".obs;
  var address = "".obs;
  var fatalDesc = "".obs;
  var descImg = <String>[].obs;
  var remark = "".obs;

  late HomeOrderConnect _homeOrderConnect;
  var remarkTextEditController = TextEditingController();
  var departments = <RepairDepartmentListObj>[].obs;
  var departmentsStringList = <String>[].obs;
  RepairDepartmentListObj? selectedDepartment;
  var department = "请选择".obs;
  var staffs = <RepairStaffListObj>[].obs;
  var staffsStringList = <String>[].obs;
  RepairStaffListObj? selectedStaff;
  var staff = "请选择".obs;

  @override
  void onInit() {
    _homeOrderConnect = Get.find<HomeOrderConnect>();
    _orderEntity = Get.arguments;
    orderId = _orderEntity.id;
    woSn.value = _orderEntity.woSn;
    fatalType.value = _orderEntity.fatalType;
    address.value = _orderEntity.address;
    remarkTextEditController.addListener(() {
      remark.value = remarkTextEditController.text;
    });
    super.onInit();
    _loadNetworkData();
  }

  Future<void> _loadNetworkData() async {
    PropertyOrderDetailEntity orderDetailEntity =
        await _homeOrderConnect.getOrderDetail(orderId: orderId);
    PropertyOrderDetailObj orderDetailObj = orderDetailEntity.obj;
    fatalType.value = orderDetailObj.fatalType;
    address.value = orderDetailObj.address;
    fatalDesc.value = orderDetailObj.fatalDesc;
    descImg.value = orderDetailObj.pics;
  }

  Future<void> orderTransfer() async {
    if (selectedDepartment == null || selectedStaff == null) {
      ToastUtils.showToast(tip: "请先选择接单部门人员");
      return;
    }

    var response = await _homeOrderConnect.orderTransfer(
      orderId: orderId,
      repairDepartmentId: selectedDepartment!.id,
      repairStaffId: selectedStaff!.id,
      mark: remark.value,
    );
    if (response == null) {
      return;
    }
    if (response.code == ResponseCode.success) {
      Get.back(result: Constant.resultOK);
    } else {
      DialogUtil.showNetworkFail();
    }
  }

  Future<void> getDepartmentList() async {
    RepairDepartmentListEntity repairDepartmentListEntity =
        await _homeOrderConnect.getRepairDepartmentList();
    departments.value = repairDepartmentListEntity.obj;
    departmentsStringList.value = departments.map((e) => e.name).toList();
  }

  Future<void> getStaffList() async {
    if (selectedDepartment != null) {
      RepairStaffListEntity repairStaffListEntity = await _homeOrderConnect
          .getRepairStaffList(did: selectedDepartment!.id);
      staffs.value = repairStaffListEntity.obj;
      staffsStringList.value = staffs.map((e) => e.name).toList();
    }
  }

  selectDepartment(RepairDepartmentListObj departmentListObj) {
    if (selectedDepartment == null ||
        departmentListObj.id != selectedDepartment?.id) {
      selectedDepartment = departmentListObj;
      department.value = departmentListObj.name;
      selectedStaff = null;
      staff.value = "请选择";
    }
  }

  selectStaff(RepairStaffListObj staffListObj) {
    if (selectedStaff == null || staffListObj.id != selectedStaff?.id) {
      selectedStaff = staffListObj;
      staff.value = staffListObj.name;
    }
  }

  void clickImageItem(String path) {
    FocusManager.instance.primaryFocus?.unfocus();
    Get.to(
      () => DisplayPictureScreen(
        imagePath: path,
        isShowRetake: false,
      ),
    );
  }
}
